package com.good.webadmin.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.good.domain.controller.BaseController;
import com.good.domain.entity.AdminRoleModule;
import com.good.domain.models.PostID;
import com.good.domain.models.Result;
import com.good.domain.service.IAdminLogService;
import com.good.domain.service.IAdminRoleModuleService;
import com.good.webadmin.Models.Params.ParAddRoleModule;
import com.good.webadmin.Models.TokenUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqz
 * @since 2022-03-05
 */
@RestController
@RequestMapping("/webadmin/admin-role-module")
@Api( tags = "角色功能模块管理")
public class AdminRoleModuleController extends BaseController {
    @Autowired
    private IAdminRoleModuleService iAdminRoleModuleService;
    @Autowired
    private IAdminLogService adminLogService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation("添加")
    public Result Add(TokenUser tokenUser, @RequestBody ParAddRoleModule parAddRoleModule){
        QueryWrapper<AdminRoleModule> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id",parAddRoleModule.getRoleId());
        queryWrapper.eq("module_id",parAddRoleModule.getModuleId());
        var rolemodile=iAdminRoleModuleService.getOne(queryWrapper);
        if(rolemodile==null){
            rolemodile=new AdminRoleModule();
            rolemodile.setModuleId(parAddRoleModule.getModuleId());
            rolemodile.setRoleId(Integer.valueOf(parAddRoleModule.getRoleId()));
            rolemodile.setUid(tokenUser.getUserId());
            var ret=iAdminRoleModuleService.save(rolemodile);
            if(ret){
                return  Success("添加角色的模块成功");
            }else
            {
                return OtherError("添加角色的模块成功");
            }
        }else
        {
            return OtherError("已经添加过了");
        }
    }

    @RequestMapping(value = "/AddBatch",method = RequestMethod.POST)
    @ApiOperation("添加")

    public  Result AddBatch(TokenUser tokenUser, @RequestBody ParAddRoleModule parAddRoleModule){

        if(parAddRoleModule.getRoleId()!="") {
            QueryWrapper<AdminRoleModule> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("role_id",parAddRoleModule.getRoleId());
            iAdminRoleModuleService.remove(queryWrapper);
            var roles = parAddRoleModule.getRoleId().split(",");
            for (int i = 0; i < roles.length; i++) {

                AdminRoleModule rolemodile = new AdminRoleModule();
                rolemodile.setModuleId(parAddRoleModule.getModuleId());
                rolemodile.setRoleId(Integer.valueOf(roles[i]));
                rolemodile.setUid(tokenUser.getUserId());
                var ret = iAdminRoleModuleService.save(rolemodile);
            }
            return Success("ok");
        }else
        {
            return NoParamError("缺少参数");
        }

    }
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ApiOperation("删除角色的模块")
    public Result Del(TokenUser tokenUser, @RequestBody PostID postID)
    {
        if(postID.getId()==null){
            return NoParamError("id不能为空");
        }else
        {
            var ret=iAdminRoleModuleService.removeById(postID.getId());
            if(ret){
                return Success("删除模块成功");
            }
            else {
                return  Error500("删除模块失败");
            }
        }
    }
    @PostMapping("listByRole")
    @ApiOperation("查询角色模块")
    public Result ListByRole(@RequestBody PostID postID){
        QueryWrapper<AdminRoleModule> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id",postID.getId());
        var modules=iAdminRoleModuleService.list(queryWrapper);
        return Success(modules);
    }


}
